Source Code Viewer - Example 3 - ViewCountryListAction

package name.matthewgreet.example3.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

import name.matthewgreet.example3.common.AbstractViewActionSupport;
import name.matthewgreet.example3.common.BrowserTabSession;
import name.matthewgreet.example3.common.ListCache;
import name.matthewgreet.example3.common.ListCache.ListCacheConfig;
import name.matthewgreet.example3.common.ListCache.PaginationMode;
import name.matthewgreet.example3.common.ListFinder;
import name.matthewgreet.example3.form.FindCountryByFirstCharForm;
import name.matthewgreet.example3.form.FindCountryByNameContainsForm;
import name.matthewgreet.example3.view.CountryListDisplay;
import name.matthewgreet.example3.vo.CountryList;
import name.matthewgreet.example3.vo.CountryDTO;

/**
 * Displays search results.  
 */
@InterceptorRef("ViewStack")
@Results({
	@Result(name=ActionSupport.SUCCESS, location="/pages/CountryList.jsp")
})
public class ViewCountryListAction extends AbstractViewActionSupport {
	public static class CountryListByFirstCharFinder implements ListFinder<CountryDTO> {
		private static final long serialVersionUID = 8357909989698034674L;
		
		private BrowserTabSession browserTabSession;
		private char firstChar;

		public CountryListByFirstCharFinder(BrowserTabSession browserTabSession, char firstChar) {
			super();
			this.browserTabSession = browserTabSession;
			this.firstChar = firstChar;
		}

		@Override
		public List<CountryDTO> getList() throws Exception {
			return CountryList.getInstance(browserTabSession).findByFirstLetter(firstChar);
		}

		public char getFirstChar() {
			return firstChar;
		}
	}
	
	public static class CountryListByPartialNameFinder implements ListFinder<CountryDTO> {
		private static final long serialVersionUID = 8357909989698034674L;
		
		private BrowserTabSession browserTabSession;
		private String partialName;

		public CountryListByPartialNameFinder(BrowserTabSession browserTabSession, String partialName) {
			super();
			this.browserTabSession = browserTabSession;
			this.partialName = partialName;
		}

		@Override
		public List<CountryDTO> getList() throws Exception {
			return CountryList.getInstance(browserTabSession).findByPartialName(partialName);
		}

		public String getPartialName() {
			return partialName;
		}
	}
	
	private static final long serialVersionUID = -7979376337555804889L;
	
	private static final String ATTR_NAME_LIST_CACHE	= ViewCountryListAction.class + "_LIST_CACHE";
	private static final int PAGE_SIZE 	= 20;
	
	public static final String RESULT_RESTART = "restart";
	
	@SuppressWarnings("unchecked")
	public static ListCache<Integer,CountryDTO> retrieveListCache(BrowserTabSession session) {
		ListCache<Integer,CountryDTO> result;
		ListCacheConfig<Integer,CountryDTO> config;
		
		result = (ListCache<Integer,CountryDTO>)session.getAttribute(ATTR_NAME_LIST_CACHE);
		if (result == null) {
			config = new ListCacheConfig<Integer,CountryDTO>();
			config.setKeyExtractor(c -> c.getId());
			config.setListFinder(new CountryListByFirstCharFinder(session, 'A'));
			config.setName("countries");
			config.setPageSize(PAGE_SIZE);
			config.setPaginationMode(PaginationMode.FULL_LIST);
			result = new ListCache<Integer,CountryDTO>(config);
			session.setAttribute(ATTR_NAME_LIST_CACHE, result);
		}
		return result;
	}
	
	
	private Integer pageNo;
	
	private FindCountryByFirstCharForm searchByFirstCharForm;
	private FindCountryByNameContainsForm searchByNameContainsForm;
	
	private CountryListDisplay listDisplay;
	
	@Actions({
		@Action("/"),
    	@Action("/ViewCountryList")
	})
    @Override
    public String execute() throws Exception {
		CountryListByFirstCharFinder countryListByFirstCharFinder;
		CountryListByPartialNameFinder countryListByPartialNameFinder;
		BrowserTabSession  session;
    	ListCache<Integer,CountryDTO> listCache;
    	
    	session = getBrowserTabSession();
    	listCache = retrieveListCache(session);
    	if (listCache.getListFinder() instanceof CountryListByFirstCharFinder) {
    		countryListByFirstCharFinder = (CountryListByFirstCharFinder)listCache.getListFinder();
    		searchByFirstCharForm = new FindCountryByFirstCharForm(countryListByFirstCharFinder.getFirstChar());
    		searchByNameContainsForm = new FindCountryByNameContainsForm();
    	} else if (listCache.getListFinder() instanceof CountryListByPartialNameFinder) {
    		countryListByPartialNameFinder = (CountryListByPartialNameFinder)listCache.getListFinder();
    		searchByFirstCharForm = new FindCountryByFirstCharForm();
    		searchByNameContainsForm = new FindCountryByNameContainsForm(countryListByPartialNameFinder.getPartialName());
    	} else {
    		searchByFirstCharForm = new FindCountryByFirstCharForm();
    		searchByNameContainsForm = new FindCountryByNameContainsForm();
    	}
	    if (pageNo != null) {
	    	listCache.setPageNo(pageNo);
	    }
    	listDisplay = new CountryListDisplay(listCache);
    	listDisplay.init();
		return SUCCESS;
    }

	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public FindCountryByFirstCharForm getSearchByFirstCharForm() {
		return searchByFirstCharForm;
	}
	public FindCountryByNameContainsForm getSearchByNameContainsForm() {
		return searchByNameContainsForm;
	}
    
	public CountryListDisplay getListDisplay() {
		return listDisplay;
	}


}